package p2p.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JTextArea;

import p2p.tracker.Tracker;
import p2p.tracker.TrackerListener;
import p2p.tracker.client.ClientManager;
import p2p.tracker.client.GraphListener;
//import org.jgraph.JGraph;
//import org.jgraph.graph.DefaultEdge;
//import org.jgraph.graph.DefaultGraphCell;
//import org.jgraph.graph.DefaultGraphModel;
//import org.jgraph.graph.DefaultPort;
//import org.jgraph.graph.GraphConstants;
//import org.jgraph.graph.GraphModel;
//import org.jgrapht.ListenableGraph;
//import org.jgrapht.ext.JGraphModelAdapter;
//import org.jgrapht.graph.ListenableDirectedGraph;

public class UiTracker extends JFrame implements TrackerListener, GraphListener {

	private JTextArea logging;

	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {

		UiTracker ui = new UiTracker();
		ui.initTracker();
		ui.setSize(800, 600);
		ui.setLocationRelativeTo(null);
		ui.setDefaultCloseOperation(EXIT_ON_CLOSE);
		ui.setVisible(true);

	}

	private void initTracker() {

		Tracker.getInstance().addListener(this);
		ClientManager.getInstance().addListener(this);

	}

	public UiTracker() {

		initTracker();

		logging = new JTextArea(5, this.getWidth());
		UITrackerBuilder builder = new UITrackerBuilder(Tracker.getInstance(),
				this, this.logging);
		builder.build();

		builder.addActionListener(0, new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				if (!Tracker.getInstance().isRunning()) {
					new Thread(Tracker.getInstance(), "Tracker").start();

				}

			}

		});

		builder.addActionListener(1, new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				Tracker.getInstance().stopTracker();

			}
		});
		
		builder.addActionListener(2, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ClientManager.getInstance().createGraphVisualization();
			}
		});
	}

	@Override
	public void logEvent(String logEvent) {
		String current = this.logging.getText() + "\n" + logEvent;
		this.logging.setText(current);
	}

	@Override
	public void updateGraphVisualisation() {
		//TODO Implement updateGraphVisualisation
	}

}
